package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/7/8 9:28
 * https://leetcode.cn/problems/island-perimeter/
 */
public class Title463II {

    int n, m, count;

    public int islandPerimeter(int[][] grid) {
        n = grid.length;
        m = grid[0].length;
        count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    //上
                    if (i == 0 || grid[i - 1][j] == 0) {
                        count++;
                    }
                    //下
                    if (i == n - 1 || grid[i + 1][j] == 0) {
                        count++;
                    }
                    //左
                    if (j == 0 || grid[i][j - 1] == 0) {
                        count++;
                    }
                    //右
                    if (j == m - 1 || grid[i][j + 1] == 0) {
                        count++;
                    }
                }
            }
        }
        return count;
    }



    public static void main(String[] args) {

    }
}
